﻿#include "Art3062LampControl.h"
#include "DAME3000.h"
#include <QDebug>

Art3062LampControl::Art3062LampControl()
{
    m_hDevice = INVALID_HANDLE_VALUE;
}

Art3062LampControl::~Art3062LampControl()
{
    if(m_hDevice != INVALID_HANDLE_VALUE)
    {
        DAME3000_ReleaseDevice(m_hDevice);
    }
}

bool Art3062LampControl::valueRange(int channel, int &min, int &max)
{
    Q_UNUSED(channel);
    min = 0;
    max = 4096;
    return true;
}

bool Art3062LampControl::setEnable(int channel, bool on)
{
    Q_UNUSED(channel);
    Q_UNUSED(on);
    return true;
}

bool Art3062LampControl::connectDevice(QString ip, int port)
{
    m_hDevice = DAME3000_CreateDevice(ip.toLatin1().data(), port, 100, 100);
    if(m_hDevice == INVALID_HANDLE_VALUE)
    {
        qDebug() << "ART_LED error: INVALID_HANDLE_VALUE";
        return false;
    }
    char ModuleInfo[256];
    if(!DAME3000_GetDeviceVersion(m_hDevice, ModuleInfo))
    {
        return false;
    }
    else
    {
        return true;
    }
}

bool Art3062LampControl::setValue(int channel, int intensity)
{
    if(m_hDevice == INVALID_HANDLE_VALUE)
    {
        qDebug() << "ART_LED error: INVALID_HANDLE_VALUE";
        return false;
    }
    intensity = qBound(0, intensity, 4095);
    if(DAME3000_WriteDeviceDA(m_hDevice, intensity, channel))
    {
        return true;
    }
    else
    {
        qDebug() << "ART_LED error: DAME3000_WriteDeviceDA";
        return false;
    }
}

int Art3062LampControl::value(int channel)
{
    if(m_hDevice == INVALID_HANDLE_VALUE)
    {
        qDebug() << "ART_LED error: INVALID_HANDLE_VALUE";
        return 0;
    }
    long v;
    if(DAME3000_GetDeviceDAVal(m_hDevice, &v, channel))
    {
        return v;
    }
    else
    {
        qDebug() << "ART_LED error: DAME3000_GetDeviceDAVal";
        return 0;
    }
}
